package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.product.model.BrandModel;
import com.ccjq.shosestore.product.model.ProductModel;
import com.ccjq.shosestore.product.service.ProductService;
import com.ccjq.shosestore.product.service.impl.ProductServiceImpl;
import com.ccjq.shosestore.product.util.PageBean;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ProductServlet")
public class ProductServlet extends HttpServlet {
    private static final String FALG_PRO = "index";//首页
    private static final String FALG_SHOP = "product";//商品
    private static final String DETAILED = "detailed";//详情页
    private static final String BRAND = "brand"; // 去品牌页
    private static final String ADDCARD = "add";// 去购物车(单个商品添加)
    private static final String ADDCARDS = "adds";// 去购物车(详情页 需要添加数量的商品添加)
    public ProductServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = request.getParameter("flag");
        System.out.println("flag = " + flag);
        if (StringUtils.isBlank(flag)) {
            toProPage(request, response);
        } else if (FALG_PRO.equals(flag)) {
            toProductPage(request, response);
        } else if (FALG_SHOP.equals(flag)) {
            // 去商品页(有分页)
            toProPage(request, response);
        } else if (DETAILED.equals(flag)) {
            // 去详情页
            toDetailedPage(request, response);
        } else if (BRAND.equals(flag)) {
            // 去品牌页(有分页)
            toBandPage(request, response);
        } /*else if (ADDCARD.equals(flag)) {
			// 去购物车(单个商品添加)
			toAddCard(request, response);
		} else if (ADDCARDS.equals(flag)) {
			// 去购物车(详情页 需要添加数量的商品添加)
			toAddCards(request, response);
		}*/
    }

    private void toProductPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductService p = new ProductServiceImpl();
        List<ProductModel> newp = p.getNewproductList();// 获取新品区List
        List<ProductModel> hot = p.getFashionList();// 获取热卖区List
        List<ProductModel> scroll = p.getScroll();// 获取滚动List
        List<BrandModel> brand = p.getBrandTextList();//获取品牌列表
        request.setAttribute("scroll", scroll);
        request.getSession().setAttribute("hot", hot);
        request.setAttribute("newp", newp);
        request.getSession().setAttribute("brand", brand);
        request.getRequestDispatcher("WEB-INF/jsp/home/firstPage.jsp").forward(request, response);
    }

    ProductService p = new ProductServiceImpl();


    private void toProPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取分页后的list
        String currentPage = ""; // 当前页 如果空则默认显示第一页
        String pageMethod = ""; // 用户动作 如果空则显示第currentPage页
        currentPage = request.getParameter("currentPage");
        pageMethod = request.getParameter("pageMethod");
        List<ProductModel> list = p.getProductList(pageMethod, currentPage);
        // 获取热卖
        List<ProductModel> hotlist = p.getFashionList();
        request.setAttribute("hot", hotlist);// 热卖
        request.setAttribute("totalPages", PageBean.getTotalPages());// 总页数
        request.setAttribute("currentPage", PageBean.getCurrentPage());// 当前页
        request.setAttribute("pagesize", PageBean.getPageSize());//每页记录数
        request.setAttribute("prolist", list);// 全部商品信息

        request.setAttribute("fen", "product");// 分页标识
        // 转发

        request.getRequestDispatcher("WEB-INF/jsp/product/product_list.jsp").forward(request, response);
    }

    /**
     * 显示某一个商品的详细信息
     */
    private void toDetailedPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductService p = new ProductServiceImpl();
        String product_id = request.getParameter("product_id");
        // 获取jsp所需的信息
        ProductModel product = p.getProduct(product_id);// 获取商品信息
        String stock = p.getProductQuantity(product_id) + "";// 获取库存
        List<ProductModel> list = p.getBrowseList();// 获取最近浏览List
        List<ProductModel> hot = p.getFashionList();// 获取热卖区List
        String qu = p.getQu(product_id);// 获取销量
        if (qu == null) {// 当销量为空时 手动赋值
            qu = "0";
        }
        // 将信息存入HttpSesrequest对象 通过response发到jsq页面
        request.setAttribute("hot", hot);
        request.setAttribute("qu", qu);
        request.setAttribute("brow", list);
        request.setAttribute("stock", stock);
        request.setAttribute("product", product);
        // 转发
        request.getRequestDispatcher("WEB-INF/jsp/product/product_detail.jsp").forward(request, response);
    }

    /**
     * 显示某一品牌的所有商品列表
     */
    private void toBandPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String category_id = request.getParameter("category_id");
        ProductService p = new ProductServiceImpl();

        String currentPage = ""; // 当前页 如果空则默认显示第一页
        String pageMethod = ""; // 用户动作 如果空则显示第currentPage页
        currentPage = request.getParameter("currentPage");
        pageMethod = request.getParameter("pageMethod");
        request.setAttribute("pagesize", PageBean.getPageSize());//每页记录数
        // 清空可能存在的旧Sesrequest
        request.setAttribute("prolist", null);
        // 获取信息
        System.out.println("pageMethod:" + pageMethod + "  /  currentPage:" + currentPage);
        List<ProductModel> list = p.getBrand(category_id, pageMethod, currentPage);

        List<ProductModel> hot = p.getFashionList();// 获取热卖区List
        // 将信息存入HttpSesrequest对象 通过response发到jsq页面
        request.setAttribute("hot", hot);// 热卖区list
        request.setAttribute("prolist", list);// 某品牌list
        request.setAttribute("totalPages", PageBean.getTotalPages());// 总页数
        request.setAttribute("currentPage", PageBean.getCurrentPage());// 当前页
        request.setAttribute("fen", "brand");// 分页标识
        request.setAttribute("category_id", category_id);// 品牌id
        request.getRequestDispatcher("WEB-INF/jsp/product/product_list.jsp").forward(request, response);
    }


    /**
     * 不数量的添加购物车 详情页
     * */
	private void toAddCard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 用户是否登陆验证
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		if (u != null) {
			//已经登录
			request.getRequestDispatcher("ShoppingCartServlet?flag=insert").forward(request, response);
		} else {
			// 未登录
			request.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(request, response);
		}
	}

    /**
     * 带数量的添加购物车 主页/列表页
     * */
	private void toAddCards(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 用户是否登陆验证
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		if (u != null) {
			//已登录
			request.getRequestDispatcher("ShoppingCartServlet?flag=insert").forward(request, response);
		} else {
			// 未登录
			request.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(request, response);
		}
	}
}
